package com.sjsu.cloudmanager.client.ui;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BaseListLoadResult;
import com.extjs.gxt.ui.client.data.BaseListLoader;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.*;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.sjsu.cloudmanager.client.CloudManager;
import com.sjsu.cloudmanager.client.LabManagerEvents;
import com.sjsu.cloudmanager.client.service.CloudLabServiceAsync;
import com.sjsu.cloudmanager.client.service.CloudUserAdminServiceAsync;
import com.sjsu.cloudmanager.client.ui.model.CloudLabBean;
import com.sjsu.cloudmanager.client.ui.model.CloudUserBean;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: ckempaiah
 * Date: 10/21/11
 * Time: 9:35 AM
 * To change this template use File | Settings | File Templates.
 */
public class LabUserFormPanel extends Dialog {

    FormBinding formBinding;
    FormPanel formPanel;

    @Override
    protected void onRender(Element parent, int index) {
        super.onRender(parent, index);
        setButtons("");
        setIcon(IconHelper.createStyle("user"));
        setHeading("Lab Manager Add User");
        setModal(true);
        setBodyBorder(false);
        setLayout(new FormLayout());
        setWidth(450);
        //setBodyBorder(true);
        //setWidth(300);
        FormPanel panel = new FormPanel();
        panel.setHeaderVisible(false);
        TextField<String> userId = new TextField<String>();
        userId.setName("userName");
        userId.setAllowBlank(false);
        userId.setFieldLabel("User Id");
        panel.add(userId);

        TextField<String> firstName = new TextField<String>();
        firstName.setName("userFirstName");
        firstName.setAllowBlank(false);
        firstName.setFieldLabel("First Name");
        panel.add(firstName);

        TextField<String> lastName = new TextField<String>();
        lastName.setName("userLastName");
        lastName.setAllowBlank(false);
        lastName.setFieldLabel("Last Name");
        panel.add(lastName);

        TextField<String> userEmail = new TextField<String>();
        userEmail.setName("userEmail");
        userEmail.setAllowBlank(false);
        userEmail.setFieldLabel("User Email");
        panel.add(userEmail);

        HiddenField<Integer> userDbId = new HiddenField<Integer>();
        userDbId.setName("userId");
        panel.add(userDbId);

        RadioGroup rg = new RadioGroup();
        rg.setFieldLabel("User Status");
        Radio active = new Radio();
        active.setBoxLabel("Active");
        active.setValueAttribute("active");
        active.setValue(true);
        //active.setName("active");
        Radio inactive = new Radio();
        inactive.setBoxLabel("Inactive");
        inactive.setValueAttribute("inactive");
        rg.setName("active");
        rg.add(active);
        rg.add(inactive);
        rg.setValue(active);
        rg.setSelectionRequired(true);
        panel.add(rg);


//        //Combo box for lab
//        ComboBox<CloudLabBean> cloudLabComboBox = new ComboBox<CloudLabBean>();
//        BaseListLoader<BaseListLoadResult<ModelData>> listLoader = getLoader();
//        cloudLabComboBox.setStore(new ListStore<CloudLabBean>(listLoader));
//        cloudLabComboBox.setFieldLabel("Lab");
//        cloudLabComboBox.setName("cloudLabBean");
//        cloudLabComboBox.setDisplayField("labName");
//        cloudLabComboBox.setValueField("cloudLabBean");
//        cloudLabComboBox.setAllowBlank(false);
//        //cloudLabComboBox.setTriggerAction(ComboBox.TriggerAction.ALL);
//        panel.add(cloudLabComboBox);


        final LayoutContainer container = this;
        Button save = new Button("Save");
        Button cancel = new Button("Cancel");
        cancel.addSelectionListener(new SelectionListener<ButtonEvent>() {
            @Override
            public void componentSelected(ButtonEvent ce) {
                container.hide();
            }
        });

        save.addSelectionListener(new SelectionListener<ButtonEvent>() {
            @Override
            public void componentSelected(ButtonEvent buttonEvent) {
                if (formPanel.isValid()) {

                    final CloudUserAdminServiceAsync service = (CloudUserAdminServiceAsync) Registry.get(CloudManager.USER_ADMIN_SERVICE);
                    CloudUserBean userBean = (CloudUserBean)formBinding.getModel();
                    service.saveCloudUser(userBean , new AsyncCallback<Void>() {
                        @Override
                        public void onFailure(Throwable throwable) {
                            MessageBox.alert(MessageBox.ERROR, "Failed To Save ", null);
                        }

                        @Override
                        public void onSuccess(Void aVoid) {
                            MessageBox.alert(MessageBox.INFO, "Successfully Saved", null);

                            container.hide();
                            ToolbarAction.getInstance().fireEvent(LabManagerEvents.SaveUser, new BaseEvent(LabManagerEvents.SaveUser));
                        }
                    });
                }
            }
        });
        panel.addButton(save);
        panel.addButton(cancel);

        FormButtonBinding fbb = new FormButtonBinding(panel);

        fbb.addButton(save);
        panel.setButtonAlign(Style.HorizontalAlignment.CENTER);
        formPanel = panel;
        formBinding = new FormBinding(panel, true);
        formBinding.addFieldBinding(new RadioGroupBinding(rg, "active"));
        add(panel, new FitData());
    }


    private BaseListLoader<BaseListLoadResult<ModelData>> getLoader() {
        final CloudLabServiceAsync service = (CloudLabServiceAsync) Registry.get(CloudManager.CLOUD_LAB_SERVICE);

        RpcProxy<List<CloudLabBean>> proxy = new RpcProxy<java.util.List<com.sjsu.cloudmanager.client.ui.model.CloudLabBean>>() {
            @Override
            public void load(Object loadConfig,
                             AsyncCallback<java.util.List<com.sjsu.cloudmanager.client.ui.model.CloudLabBean>> callback) {
                service.getCloudLabBeanList(callback);
            }
        };

        final BaseListLoader<BaseListLoadResult<ModelData>> loader = new BaseListLoader<BaseListLoadResult<ModelData>>(proxy);
        return loader;
    }

    public FormBinding getFormBinding() {
        return formBinding;
    }

    public FormPanel getFormPanel() {
        return formPanel;
    }

}
